package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseReq;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.Groupon;
import com.cskaoyan.bean.GrouponRules;
import com.cskaoyan.bean.groupon.*;
import com.cskaoyan.service.GoodsService;
import com.cskaoyan.service.GrouponService;
import com.cskaoyan.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: whyccz-mall-project
 * @description:
 * @create: 2021-06-09 16:56
 **/
@RestController
public class AdminGrouponController {
	@Autowired
	GrouponService grouponService;
	@Autowired
	GoodsService goodsService;

	//page=1&limit=20&goodsId=11&sort=add_time&order=desc
	@RequestMapping("admin/groupon/list")
	public BaseRespVo getGrouponList(GrouponReq grouponReq){
		GrouponListVO grouponListVO=grouponService.getGrouponList(grouponReq);
		return BaseRespVo.ok(grouponListVO);
	}

	@RequestMapping("admin/groupon/create")
	public BaseRespVo createGroupon(@Validated @RequestBody GrouponCreateBO grouponCreateBO, BindingResult bindingResult){
		Boolean flag=goodsService.contains(grouponCreateBO.getGoodsId());
		if (!flag){
			return BaseRespVo.fail(402,"参数值不对");
		}
		GrouponRules grouponRules =  grouponService.createGroupon(grouponCreateBO);
		return BaseRespVo.ok(grouponRules);
	}

	@RequestMapping("admin/groupon/update")
	public BaseRespVo updateGroupon(@Validated @RequestBody UpdateGrouponBO updateGrouponBO){
		Boolean flag=goodsService.contains(updateGrouponBO.getGoodsId());
		if (!flag){
			return BaseRespVo.fail(402,"参数值不对");
		}
		grouponService.updateGroupon(updateGrouponBO);
		return BaseRespVo.ok();
	}

	@RequestMapping("admin/groupon/delete")
	public BaseRespVo deleteGroupon(@RequestBody UpdateGrouponBO updateGrouponBO) {
		grouponService.deleteGroupon(updateGrouponBO);

		return BaseRespVo.ok();
	}

	///admin/groupon/listRecord?page=1&limit=20&sort=add_time&order=desc
	@RequestMapping("admin/groupon/listRecord")
	public BaseRespVo grouponListRecord(BaseReq baseReq){
		GrouponListRecordVO grouponListRecordVO=grouponService.getGrouponlistRecord(baseReq);
		return BaseRespVo.ok(grouponListRecordVO);
	}
}
